package cc.mrbird.febs.accounting.service.impl;

import cc.mrbird.febs.accounting.entity.Project;
import cc.mrbird.febs.accounting.entity.ProjectType;
import cc.mrbird.febs.accounting.mapper.ProjectTypeMapper;
import cc.mrbird.febs.accounting.service.IProjectTypeService;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.entity.Strings;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.utils.PermissionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 *  Service实现
 *
 * @author chenyuxuan
 * @date 2021-09-28 23:26:45
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ProjectTypeServiceImpl extends ServiceImpl<ProjectTypeMapper, ProjectType> implements IProjectTypeService {

    private final ProjectTypeMapper projectTypeMapper;

    @Override
    public IPage<ProjectType> queryProjectTypes(QueryRequest request, ProjectType projectType) {
        LambdaQueryWrapper<ProjectType> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(projectType) && StringUtils.isNotBlank(projectType.getProjectType())) {
            queryWrapper.like(ProjectType::getProjectType, projectType.getProjectType());
        }
        queryWrapper.orderByDesc(ProjectType::getId);
        Page<ProjectType> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), queryWrapper);
        List<ProjectType> records = page.getRecords();
        records.forEach(e -> {

        });
        return page;
    }

    @Override
    public List<ProjectType> findProjectTypes(ProjectType projectType) {
	    LambdaQueryWrapper<ProjectType> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        queryWrapper.orderByDesc(ProjectType::getId);
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProjectType> findOngoingProjectTypes() {
        return this.projectTypeMapper.queryOngoingProjectType();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createProjectType(ProjectType projectType) {
        projectType.setCreateBy(FebsUtil.getCurrentUser().getUsername());
        projectType.setCreateTime(new Date());
        LambdaQueryWrapper<ProjectType> queryWrapper = new LambdaQueryWrapper<>();
        List<ProjectType> projectTypes = this.getBaseMapper().selectList(queryWrapper);
        Set<String> projectTypeNames = new HashSet<>();
        for (ProjectType e : projectTypes) {
            projectTypeNames.add(e.getProjectType().trim());
        }
        if (!projectTypeNames.contains(projectType.getProjectType().trim())) {
            this.save(projectType);
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateProjectType(ProjectType projectType) {
        LambdaQueryWrapper<ProjectType> wrapper = new LambdaQueryWrapper<>();
        List<ProjectType> projectTypes = this.getBaseMapper().selectList(wrapper);
        Set<String> projectTypeNames = new HashSet<>();
        for (ProjectType e : projectTypes) {
            if (!e.getId().equals(projectType.getId())) {
                projectTypeNames.add(e.getProjectType().trim());
            }
        }
        if (!projectTypeNames.contains(projectType.getProjectType().trim())) {
            projectType.setModifyTime(new Date());
            projectType.setModifyBy(FebsUtil.getCurrentUser().getUsername());
            this.saveOrUpdate(projectType);
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteProjectType(String projectTypeIds) {
        List<String> list = Arrays.asList(projectTypeIds.split(Strings.COMMA));
        if(projectTypeMapper.ifProjectTypeUsed(list)==0){
            this.removeByIds(list);
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public ProjectType getProjectTypeById(Long projectTypeId) {
        return this.baseMapper.selectById(projectTypeId);
    }

    @Override
    public List<ProjectType> getProjectTypeByIds(List<String> projectTypeIds) {
        LambdaQueryWrapper<ProjectType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProjectType::getId, projectTypeIds);
        return this.baseMapper.selectList(queryWrapper);
    }
}
